<?php

namespace app\common\Model;

use think\Model;
use PHPMailer\PHPMailer;

class Phpmail extends Model {
	
	/*
	 * 发送邮件
	 * @param $to string 要发送的邮箱地址
	 * @param $title string 邮件标题
	 * @param $content string 邮件内容
	 * @return bool
	 */
	function sendMail($to, $title, $content) {
		require_once '../extend/PHPMailer/PHPMailerAutoload.php';
		$config = model ( 'Website' )->getAll ();
		if (count ( $config ) > 0 && $to != '' && $title != '' && $content != '') {
			$mail = new \PHPMailer ();
			// $mail->SMTPDebug = 3;
			// $mail->SMTPOptions = array ('ssl' => array ('verify_peer' => false,'verify_peer_name' => false,'allow_self_signed' => true ) );
			$mail->isSMTP (); // 启用SMTP
			$mail->Host = $config ['mail_host'];
			$mail->SMTPAuth = true; // 启用smtp认证
			$mail->Username = $config ['mail_username'];
			$mail->Password = $config ['mail_password'];
			$mail->SMTPSecure = $config ['smtp_secure'];
			$mail->Port = $config ['smtp_port'];
			$mail->setFrom ( $config ['mail_from'], $config ['mail_fromname'] );
			$mail->addAddress ( $to );
			$mail->WordWrap = 50; // 设置每行字符长度
			$mail->isHTML ( true ); // 是否HTML格式邮件
			$mail->CharSet = 'UTF-8'; // 设置邮件编码
			$mail->Subject = $title; // 邮件主题
			$mail->Body = $content; // 邮件内容
			$mail->AltBody = ""; // 邮件正文不支持HTML的备用显示
			
			if ($mail->send ())
				return 'success';
			else
				return 'fail';
			// $mail->ErrorInfo
		} else
			return 'required';
	}
}